package com.example;

import java.util.Optional;

/**
 * @author Liuqz
 * @date 2018/11/17 21:21
 */
public class OptionalInAction {


    public static void main(String[] args) {
        Optional.ofNullable(getInsuranceNameByOptional(null)).ifPresent(System.out::println);
    }


    private static String getInsuranceNameByOptional(Person person) {
        return Optional.ofNullable(person)
                .flatMap(Person::getCar)
                .flatMap(Car::getInsurance)
                .map(Insurance::getName).orElse("Unknown");
    }
}
